<?php
namespace app\index\controller;

use app\common\model\Apply;
use app\common\model\ApplyBrokerage;
use app\index\validate\ApplyBrokerageValidate;
use app\common\model\UserAccount;
use app\common\model\Record;
use app\common\model\RecordBrokerage;
use app\common\model\UserAccountRecord;
use app\common\model\Extract;

class User extends Base
{
    private $apply = [];

    // 个人中心公共部分
    private function common()
    {
        $this->apply = Apply::get(['user_id' => $this->user->id, 'aid' => $this->activity->id]);

        $this->assign('apply', $this->apply);
        if($this->apply) $this->assign('brokerage', $this->apply->brokerage);
    }

    // 提现
    public function deposit()
    {
        $this->common();
        return $this->fetch();
    }

    public function extractDeposit()
    {
        $amount = $this->request->post('amount');
        if(!$amount || !is_numeric($amount)) return $this->jsonFail('请填写合法参数');
        if($amount > $this->user->account->can_out_balance) return $this->jsonFail('提现金额大于可提现余额');
        $extract = Extract::add([
            'user_id' => $this->user->id,
            'amount' => $amount,
        ]);
        if($extract) {
            $account = $this->user->account;
            $account->save(['can_out_balance' => $account->can_out_balance - $amount]);
            return $this->jsonSuccess('提现申请成功');
        } else {
            return $this->jsonFail('提现申请失败');
        }
    }

    // 投票记录
    public function personal()
    {
        $this->common();
        // 获取我投票的
        $map = ['r.user_id' => $this->user->id, 'a.aid' => $this->activity->id];
        $oneForAll = Record::oneForAll($map);
        $this->assign('oneForAll', $oneForAll);
        // 获取投票给我的
        if($this->apply) {
            $where = ['r.apply_id' => $this->apply->id];
            $allForOne = Record::AllForOne($where);
            $this->assign('allForOne', $allForOne);
        }
        return $this->fetch();
    }

    // 流水（交易记录）
    public function record()
    {
        $this->common();
        // 获取充值 提现记录
        $_record = UserAccountRecord::all(['user_id' => $this->user->id, 'type' => ['in', [1, 0]]]);
        $record = [];
        foreach($_record as $key => $val) {
            if(!isset($record[$val['type']]))
                $record[$val['type']] = [
                    'amount' => 0,
                    'list' => []
                ];
            $record[$val['type']]['amount'] += abs($val['change']);
            $record[$val['type']]['list'][$key] = $val;
        }

        // 获取佣金记录
        // 获取我投票的
        $map = ['r.user_id' => $this->user->id, 'a.aid' => $this->activity->id];
        $oneForAll = RecordBrokerage::oneForAll($map);
        $this->assign('oneForAll', $oneForAll);
        // 获取投票给我的
        if($this->apply) {
            $where = ['r.apply_id' => $this->apply->id];
            $allForOne = RecordBrokerage::AllForOne($where);
            $this->assign('allForOne', $allForOne);
        }
        $this->assign('record', $record);
        return $this->fetch();
    }

    // 设置佣金
    public function setBrokerage()
    {
        $data = $this->request->post();
        $brokerage = new ApplyBrokerageValidate();
        if($this->user->account->balance < $data['min_money']) return $this->jsonFail('亲，您的账户余额低于设置佣金的最低值');
        if($brokerage->scene('add')->check($data)) {
            $brokerageModel = new ApplyBrokerage();
            $apply = Apply::get(['user_id' => $this->user->id, 'aid' => $this->activity->id]);
            $data['apply_id'] = $apply['id'];
            if(($result = $brokerageModel->add($data)) != null){
                return $this->jsonSuccess('设置成功', ['result' => $result]);
            }
            $errMsg = '未修改设置';
        }else{
            $errMsg = $brokerage->getError();
        }
        return $this->jsonFail($errMsg);
    }
}
